<?php
/**
 * Created by PhpStorm.
 * User: minicart
 * Date: 2018/9/26
 * Time: 21:49
 */

namespace app\models;

use think\Model;

/**
 * Class MenuModel
 * @package app\models
 */
class MenuModel extends Model
{
    protected $table = 'edu_auth_rule';

    /**
     * 订单列表 分页
     * @param array $where
     * @param int $page
     * @param int $limit
     * @param string $order
     * @return array
     * @throws \think\exception\DbException
     */
    public static function pageLists($where = [], $page = 1, $limit = 5, $order = 'id desc')
    {
        if ($where) {
            $result = self::where($where)->order($order)->paginate($limit, false, ['page' => $page]);
        } else {
            $result = self::order($order)->paginate($limit, false, ['page' => $page]);
        }
        if (!$result->total()) {
            return ['code' => 0, 'msg' => "ok", 'count' => 0, 'data' => []];
        }
        foreach ($result as $key => $value) {
            $result[$key]['title'] = "<a href='" . url('menu', ['pid' => $value['id']]) . "'>" . $value['title'] . "</a>";
            $opt = explode('/', $value['menu_name']);
            $result[$key]['controller'] = $opt[0];
            $result[$key]['action'] = $opt[1];
            $result[$key]['icon'] = "<i class='layui-icon " . $value['icon'] . "'></i>";
        }
        $result = $result->toArray();
        return ['code' => 0, 'msg' => "ok", 'count' => $result['total'], 'data' => $result['data']];
    }

    /**
     * 显示菜单
     * @param int $type
     * @param int $ruleType
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function selectAllMenu($type = 2, $ruleType = 1)
    {
        $where = array(
            'status' => 1,
            'is_menu' => 1,
			'type' => 1
        );
        if(get_current_type() == 1){
//			$auth = AuthModel::get(['id'=>1]);
//			$where['id'] = ['in',$auth['rules']];
		}elseif (get_current_type() == 2){
			$auth = AuthModel::get(['type'=>2,'status'=>1]);
			$where['id'] = ['in',$auth['rules']];
		}elseif (get_current_type() == 3){
			$auth = AuthModel::get(['type'=>3,'status'=>1]);
			$where['id'] = ['in',$auth['rules']];
		}elseif (get_current_type() == 4){
			$auth = AuthModel::get(['type'=>4,'status'=>1]);
			$where['id'] = ['in',$auth['rules']];
		}elseif (get_current_type() == 5){
			$auth = AuthModel::get(['type'=>5,'status'=>1]);
			$where['id'] = ['in',$auth['rules']];
		}
        if ($type == 2) {
            unset($where['is_menu']);
        }
        $order = 'sort asc';
        return $this->where($where)->order($order)->select();
    }

    /**
     * 根据规则id数组获取菜单
     * @param $rules_arr
     * @param $type
     * @param int $is_menu
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getMenus($rules_arr, $type, $is_menu = 1)
    {
        if ($rules_arr) {
            $where = array(
                'id' => array('in', $rules_arr),
                'is_menu' => 1,
                'status' => 1,
                'type' => $type,
            );
        } else {
            $where = array(
                'is_menu' => 1,
                'status' => 1,
                'type' => $type,
            );
        }
        return $this->where($where)->order('sort asc')->select();
    }

    /**
     * 是否为二级菜单
     * @param $id
     * @return bool
     */
    public function isSecondaryMenu($id)
    {
        $where = array(
            'id' => $id,
        );
        $value = $this->where($where)->value('pid');
        return $value == 0 ? false : true;
    }

    /**
     * 查询是否已存在的opt
     * @param $controller
     * @param $action
     * @param null $id
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function isExistOpt($controller, $action, $id = null)
    {
        $where = array(
            'menu_name' => $controller . '/' . $action,
            'status' => 1,
        );
        if ($id) {
            $where['id'] = array('neq', $id);
        }
        return $this->where($where)->find();
    }

    /**
     * 添加菜单
     * @param $data
     * @return false|int
     */
    public function addMenu($data)
    {
        $menu_info = array(
            'menu_name' => $data['controller'] ? $data['controller'] . '/' . $data['action'] : '',
            'icon' => $data['menuicon'],
            'title' => $data['menuname'],
            'pid' => $data['pid'],
            'is_menu' => $data['is_menu'],
            'condition' => $data['condition'],
        );
        return $this->save($menu_info);
    }

    /**
     * 编辑菜单
     * @param $data
     * @return MenuModel
     */
    public function editMenu($data)
    {

        $where = array(
            'id' => $data['id'],
        );
        $menu_info = array(
            'menu_name' => $data['controller'] ? $data['controller'] . '/' . $data['action'] : '',
            'icon' => $data['menuicon'],
            'title' => $data['menuname'],
            'pid' => $data['pid'],
            'condition' => $data['condition'],
        );
        unset($data['id']);
        return $this->where($where)->update($menu_info);
    }

    /**
     * 删除菜单
     * @param $id
     * @return false|int
     */
    public function delMenu($id)
    {
        $where = array(
            'id' => $id,
        );
        $data = array(
            'status' => -1,
        );
        return $this->where($where)->save($data);
    }

    /**
     * 根据id查询菜单信息
     * @param $id
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function selectMenuById($id)
    {
        $where = array(
            'id' => $id,
        );
        return $this->where($where)->find();
    }
}